/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.uacam.balam.notorrent.seeder;

/**
 *
 * @author User
 */
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

class FileSplitter {

//    public static void main(String args[]) throws Exception {
//        double numero = 0;
//        Scanner leeArch = new Scanner(System.in);
//        String nombreArch = leeArch.next();
//        File Arch = new File(nombreArch);
//        ConvierteUnidadesBytes x = new ConvierteUnidadesBytes();
//
//        numero = x.redondear(x.convierte(Arch.length()));
//
//        System.out.println("el tamaño del archivo es: " + Arch.length() + " bytes");
//        System.out.println("tamaño del archivo en MB: " + numero);
//
//        FileInputStream fis = new FileInputStream("DanteMostro.mp3");
//        int size = 1024;
//
//        byte buffer[] = new byte[size];
//
//        int count = 0;
//        while (true) {
//            int i = fis.read(buffer, 0, size);
//            if (i == -1) {
//                break;
//            }
//
//            String filename = "DanteMostro" + count + ".txt";
//            FileOutputStream fos = new FileOutputStream(filename);
//            fos.write(buffer, 0, i);
//            fos.flush();
//            fos.close();
//
//            ++count;
//        }
//    }

    public static void split(File file, File folderOutput) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);

            int size = 0xf4240;// 1 000 000 bytes aprox 1 MB
            byte buffer[] = new byte[size];
            int count = 1;
            
            while (true) {
                int i = fis.read(buffer, 0, size);

                if (i == -1) {
                    break;
                }

                String filename = file.getName() + ".00" + count + ".part";
                File fileOutput = new File(folderOutput, filename);
                
                FileOutputStream fos = new FileOutputStream(fileOutput);
                fos.write(buffer);
                fos.flush();
                fos.close();

                ++count;
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FileSplitter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FileSplitter.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fis.close();
            } catch (IOException ex) {
                Logger.getLogger(FileSplitter.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
